#include <jni.h>
#include <string>

#include "wkbean/WkJavaCall.h"
#include "utils/WkLoger.h"
#include "wkbean/WkFFmpeg.h"
#include "config/WkConfig.h"


JavaVM* javaVM =NULL;
PlayerStatus* playerStatus= NULL ;
WkJavaCall * wkJavaCall =NULL ;
WkFFmpeg* wkFFmpeg ;
pthread_t  thread_start ;

//是否可以退出 ,防止重复点击
bool isExitAble = true;


/**系统方法重新*/
extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
    LOGE("jni-onLoad.....");
    jint result = -1;
    JNIEnv *env;
    if(vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK)
    {
        return result;
    }
    javaVM = vm;
    return JNI_VERSION_1_4;
}

void * startPlay (void * data){
    WkFFmpeg * wkFFmpeg = (WkFFmpeg *) data ;
    wkFFmpeg->start() ;
    pthread_exit(&thread_start) ;
};

extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1preparePlayer(JNIEnv *env, jobject jobj, jstring source_)
{
    const char *source = env->GetStringUTFChars(source_, 0);
    LOGI("1播放地址= %s" , source) ;

    if(wkFFmpeg ==NULL)
    {
        if(wkJavaCall==NULL)
        {
            wkJavaCall = new  WkJavaCall( javaVM , env ,  &jobj);
        }
        wkJavaCall->onCallLoad(MAIN_THREAD , true) ;
        playerStatus= new PlayerStatus();
        wkFFmpeg = new WkFFmpeg(wkJavaCall ,playerStatus ,source) ;
        wkFFmpeg->preparePlayer() ;
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1startPlayer(JNIEnv *env, jobject instance)
{
    if(wkFFmpeg !=NULL)
    {
        pthread_create(&thread_start ,NULL , startPlay , wkFFmpeg) ;
    }
}



extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1pause(JNIEnv *env, jobject instance)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->pause();
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1resume(JNIEnv *env, jobject instance)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->resume();
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1stop(JNIEnv *env, jobject instance)
{

    if(!isExitAble)
    {
        return;
    }

    isExitAble = false;
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->release();
        delete(wkFFmpeg);
        wkFFmpeg = NULL;

        if(wkJavaCall != NULL)
        {
            delete(wkJavaCall);
            wkJavaCall = NULL;
        }
        if(playerStatus != NULL)
        {
            delete(playerStatus);
            playerStatus = NULL;
        }
    }
    isExitAble = true;

    //结合java层的判断 ，如果点击了下一首 ，先停止，再调用 准备下一首的播放
    jclass clz = env->GetObjectClass(instance);
    jmethodID jmid_next = env->GetMethodID(clz, "onCallNextByN", "()V");
    env->CallVoidMethod(instance, jmid_next);

}


extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1seek(JNIEnv *env, jobject instance, jint secds)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->seek(secds);
    }
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_wk_playersdk_WkPlayer_n_1duration(JNIEnv *env, jobject instance)
{
    if(wkFFmpeg != NULL)
    {
        return wkFFmpeg->duration;
    }
    return 0;
}


extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1volume(JNIEnv *env, jobject instance, jint percent)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->setVolume(percent);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1mute(JNIEnv *env, jobject instance, jint mute)
{

    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->setMute(mute);
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1pitch(JNIEnv *env, jobject instance, jfloat pitch)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->setPitch(pitch);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1speed(JNIEnv *env, jobject instance, jfloat speed)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->setSpeed(speed);
    }
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_wk_playersdk_WkPlayer_n_1samplerate(JNIEnv *env, jobject instance)
{
    if(wkFFmpeg != NULL)
    {
        return wkFFmpeg->getSampleRate();
    }
    return 0;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1startstoprecord(JNIEnv *env, jobject instance, jboolean start) 
{

    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->startStopRecord(start);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_wk_playersdk_WkPlayer_n_1setCirclePlay(JNIEnv *env, jobject instance, jboolean isCircle)
{
    if(wkFFmpeg != NULL)
    {
        wkFFmpeg->setCircle(isCircle);
    }


}